package com.example.concurrent.chapter06;

import java.util.concurrent.TimeUnit;

/**
 * @author Create By Liuqz
 * @date 2018/11/22 14:52
 *
 *  线程的最大优先级,不能高于线程组的最大优先级, 如果修改会怎样
 */
public class ThreadGroupPriority {

    public static void main(String[] args) {
        /**
         * create Thread Group and Thread
         */
        ThreadGroup group = new ThreadGroup("group1");

        Thread thread = new Thread(group, () -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "thread");

        thread.setDaemon(true);
        thread.start();

        System.out.println("group.getMaxPriority() = " + group.getMaxPriority());

        System.out.println("thread.getPriority() = " + thread.getPriority());

        //修改group的线程优先级
        group.setMaxPriority(3);

        System.out.println("group.getMaxPriority() = " + group.getMaxPriority());

        System.out.println("thread.getPriority() = " + thread.getPriority());

        /**
         * result :
         * group.getMaxPriority() 10
         * thread.getPriority() 5
         * group.getMaxPriority() 3
         * thread.getPriority() 5
         *
         * 虽然会出现了已经加入该group线程的优先级大于group最大优先级的情况,
         * 但是后面加入该group的线程再不会大于新设置的值 : 3
         */
    }
}
